package minidb.type; 
 
import static minidb.type.Const.TYPE;

public class fieldinfo {
	private TYPE type;
	private int length;
	private int position;
	private String name;
	
	public fieldinfo(String name, TYPE type, int length, int position){
		this.name = name;
		this.type = type;
		this.length = length;
		this.position = position;
	}
	
	public fieldinfo(java.nio.ByteBuffer buf){
		int size = (int)buf.get();
		byte []bt = new byte[size];
		buf.get(bt);
		this.name = new String(bt);
		this.type = Const.toType(buf.getInt());
		this.length = buf.getInt();
		this.position = buf.getInt();
	}
	
	public TYPE type(){ return this.type; }
	public int length(){ return this.length; }
	public int position(){ return this.position; }
	public String name(){ return this.name; }
	public void type(int type){ this.type = Const.toType(type); }
	public void length(int length){ this.length = length; }
	public void position(int position){ this.position = position; }
	public void name(String name){ this.name = name; }
	
	public byte[] array(){
		int size = name.length()+3*4+1;
		java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocateDirect(size);
		buf.rewind();
		buf.put((byte)name.length());
		buf.put(name.getBytes());
		buf.putInt(Const.toInt(type));
		buf.putInt(this.length);
		buf.putInt(this.position);
		buf.rewind();
		byte []bt = new byte[size];
		buf.get(bt);
		return bt;
	}
	
}
